home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * Copyright (C) 1991, Silicon Graphics, Inc.
- * All Rights Reserved.
- */
- #include <math.h>
- #include "fft.h"
- #include <malloc.h>
- #include <stdio.h>
- /*
- ** Instructions:
- **
- ** First call buildtable to create the sin/cos tables.
- ** buildtable(ln) where ln is the log base 2 of the number of
- ** samples.
- ** Next call bitreverse to bitreverse the input data.
- ** bitreverse(x,ln) where ln is the log base 2 of the number of
- ** samples and x is a complex array which holds
- ** the samples.
- ** Finally, call fft to perform a forward fft, or call ifft to
- ** perform an inverse fft.
- ** fft(x,ln)
- ** ifft(x,ln) where ln is the log base 2 of the number of
- ** samples and x is a complex array which holds
- ** the samples.
- ** If you are performing several fft/iffts of the same size, you only
- ** need to build the tables once
- */
- #include <math.h>
-
-
- /*******
- // Object
- // To read in a sampled sound file
- // compute an estimate of the power Spectrum of a Delta t
- // write out the pse for each time interval
- // pse should be normalized to 0 to 1.0
- // Question should we plot log(power) or power???
- /*******/
-
- static complex *c=0;
-
- #define PI2 6.283185307179586476925287
-
- int pwr_two[] = {
- 1, 2, 4, 8,
- 16, 32, 64, 128,
- 256, 512, 1024, 2048,
- 4096, 8192, 16384, 32768,
- 65536, 131072, 262144, 524288,
- 1048576, 2097152, 4194304, 8388608,
- 16777216, 33554432, 67108864, 134217728,
- 268435456, 536870912, 1073741824
- };
-
- void NRICfft( float *, unsigned int, int);
-
-
-
- #define FILTERSIZE 16384
- #define FILTERSIZE2 32768
- #define LOGFILTERSIZE2 15
-
- #define PI2 6.283185307179586476925287
- int nfreqs, nintervals;
-
- #define MAXFREQS 256
- #define MAXINTERVALS 2000
- #define MAXSAMPS 8192
-
- complex tmpc[8192];
-
- typedef signed short SAMPLE;
-
- int fftinitialized =0;
-
-
- float spectrum( SAMPLE * samples, int nsamples, float * ps, int nfreqs, float maxpwr )
- {
- int i,lognsamples,nfft;
- SAMPLE * pb;
- float pwr,nfftsqd;
- complex * pc;
- nfft = 1;
- lognsamples = 0;
- while (nfft < nsamples) { nfft<<=1; lognsamples++;}
- if ( fftinitialized != lognsamples)
- {
- buildtable(lognsamples);
- fftinitialized = lognsamples;
- }
-
-
- for(pb = samples, pc = tmpc, i = 0; i < nsamples ; i+=1)
- { pc->r = (float) *pb++;
- pc->i = 0.0; pc++;
- }
- if (nfft > nsamples ) /* zero fill /***/
- for(i = 0; i < nfft-nsamples ; i++)
- { pc->r = 0.0; pc->i = 0.0 ; pc++; }
- pc = tmpc;
-
- bitreverse(tmpc,lognsamples);
- fft(tmpc,lognsamples);
- nfftsqd = (float)(nfft)*(float)(nfft);
-
- for(i=1; i<nfreqs; i++)
- {
-
- pwr = tmpc[i].r * tmpc[i].r;
- pwr += tmpc[i].i * tmpc[i].i;
- pwr = fsqrt(pwr/nfftsqd) ;
- ps[i-1] = pwr;
- if (pwr > maxpwr) maxpwr = pwr;
- }
- return(maxpwr);
- }
-
-
-
-
-
-
- void buildtable(int ln)
- {
- int n,halfn,i;
- float theta;
- n=pwr_two[ln];
- halfn=pwr_two[ln-1];
- if(c)free(c);
- c=(complex *)malloc(halfn*sizeof(complex));
- if(c==0){printf("malloc failed\n");exit(0);}
- for(i=0;i<halfn;i++) {
- theta= PI2*i/n;
- c[i].r=cos(theta);
- c[i].i= -sin(theta);
- }
- }
-
- void bitreverse(complex *x,int ln)
- {
- int i,j,k,n;
- complex hold;
-
- n=pwr_two[ln];
- k = 0;
- j=0;
- for (;;) {
- if (k > j) {
- hold.r = x[j].r;
- hold.i = x[j].i;
-
- x[j].r = x[k].r;
- x[j].i = x[k].i;
-
- x[k].r = hold.r;
- x[k].i = hold.i;
- }
- j++;
- if(j==n)break;
-
- /* k = k+1, (k is n bits long and bit reversed). */
-
- i=ln-1;
- while(i>=0) {
- if (k & pwr_two[i])
- k = k - pwr_two[i];
- else
- break;
- i--;
- }
-
- k = k + pwr_two[i];
- }
- }
-
- void fft(complex *x, int ln)
- {
- unsigned int stuckbit,stuckbitcomplement,halfn,t,b;
- unsigned int shft,msk,wt;
- unsigned int c0,c1;
- unsigned int n;
- float qr, qi, xtr, xti, xbr, xbi, cwtr, cwti;
-
- n=pwr_two[ln];
- msk=n-1;
- halfn=pwr_two[ln-1];
- shft=ln-1;
-
- stuckbit=1;
- c0=ln;
- while(c0--){
- t=0;
- stuckbitcomplement = ~stuckbit;
- c1=halfn;
- while(c1--) {
- wt=(t<<shft)&msk;
- b=t+stuckbit;
-
- xbr = x[b].r;
- xbi = x[b].i;
- cwtr = c[wt].r;
- cwti = c[wt].i;
-
- qr = xbr * cwtr - xbi * cwti;
- qi = xbr * cwti + xbi * cwtr;
-
- xtr = x[t].r;
- xti = x[t].i;
-
- x[b].r = xtr-qr;
- x[b].i = xti-qi;
-
- x[t].r = xtr + qr;
- x[t].i = xti + qi;
-
- t=(b+1) & stuckbitcomplement;
- }
- stuckbit <<= 1;
- shft -= 1;
- }
- }
-
- void ifft(complex *x,int ln)
- {
- unsigned int stuckbit,stuckbitcomplement,halfn,t,b;
- unsigned int shft,msk,wt;
- unsigned int c0,c1;
- unsigned int n;
- float qr,qi;
-
- n=pwr_two[ln];
- msk=n-1;
- halfn=pwr_two[ln-1];
- shft=ln-1;
-
- stuckbit=1;
- c0=ln;
- while(c0--){
- t=0;
- stuckbitcomplement = ~stuckbit;
- c1=halfn;
- while(c1--){
- b=t+stuckbit;
- wt=(t<<shft)&msk;
-
- qr = x[b].r * c[wt].r + x[b].i * c[wt].i;
- qi = -(x[b].r * c[wt].i) + x[b].i * c[wt].r;
-
- x[b].r = x[t].r-qr;
- x[b].i = x[t].i-qi;
-
- x[t].r += qr;
- x[t].i += qi;
-
- t=(b+1) & stuckbitcomplement;
- }
- stuckbit <<= 1;
- shft -= 1;
- }
- }
-